package com.example.demo.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auth0.jwt.JWT;

import com.example.demo.Aspect.ValidatedSending;
import com.example.demo.GeneralResponse.APIResponse;
import com.example.demo.Service.MessageService;
import com.example.demo.Service.TokenService;

/**
 * @version: V0.3
 * @author: 郑皓杰 吴嘉鑫
 * @className: MessageController
 * @packageName: com.example.demo.Controller
 * @description: 这是进行求职招聘信息发布上传的控制类
 * @data: 2021-07-20
 **/


@RestController
@RequestMapping("/Message")
public class MessageController {
	@Autowired
	private MessageService mesService;
	
	@Autowired
	private TokenService tokenService;
	
	/**
	* @author:吴嘉鑫
	* @methodsName: getLatestHr
	* @description: 返回最新招聘信息
	* @param:  HttpServletRequest httpServletRequest
	* @param:  HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/getLatestHr")
	public APIResponse<?> getLatestHr(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		return mesService.getLatestHr();			
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: getAllHr
	* @description: 返回所有招聘信息
	* @param:  HttpServletRequest httpServletRequest
	* @param:  HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/getAllHr")
	public APIResponse<?> getAllHr(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		return mesService.getAllHr();
				
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: getLatestEmp
	* @description: 返回最新求职信息
	* @param:  HttpServletRequest httpServletRequest
	* @param:  HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/getLatestEmp")
	public APIResponse<?> getLatestEmp(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		return mesService.getLatestEmp();
				
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: getAllEmp
	* @description: 返回所有求职信息
	* @param:  HttpServletRequest httpServletRequest
	* @param:  HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/getAllEmp")
	public APIResponse<?> getAllEmp(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		return mesService.getAllEmp();
				
	}
	
	/**
	* @author:吴嘉鑫 郑皓杰
	* @methodsName: sendHrMessage
	* @description: hr发布招聘信息
	* @param:  HttpServletRequest httpServletRequest
	* @param:  HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@ValidatedSending
	@CrossOrigin
	@RequestMapping("/sendHrMessage")
	public APIResponse<?> sendHrMessage(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		String token = httpServletRequest.getHeader("Token");
		String username = JWT.decode(token).getClaim("username").asString();
	    String text=httpServletRequest.getParameter("text");
	    String position=httpServletRequest.getParameter("position");
	    Integer salary=Integer.parseInt(httpServletRequest.getParameter("salary"));
	    return mesService.sendHrMes(username,salary,position,text);
				
	}
	
	/**
	* @author:吴嘉鑫 郑皓杰
	* @methodsName: sendEmpMessage
	* @description: emp发布求职信息
	* @param:  HttpServletRequest httpServletRequest
	* @param:  HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@ValidatedSending
	@CrossOrigin
	@RequestMapping("/sendEmpMessage")
	public APIResponse<?> sendEmpMessage(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		String token = httpServletRequest.getHeader("Token");
		String username = JWT.decode(token).getClaim("username").asString();
	    String text=httpServletRequest.getParameter("text");
	    String position=httpServletRequest.getParameter("position");
	    Integer salary=Integer.parseInt(httpServletRequest.getParameter("salary"));
	    return mesService.sendEmpMes(username,salary,position,text);
	}
	
	/**
	* @author:吴嘉鑫 郑皓杰
	* @methodsName: hrgetmymes
	* @description: hr获取自己信息
	* @param:  HttpServletRequest httpServletRequest
	* @param:  HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@ValidatedSending
	@CrossOrigin
	@RequestMapping("/hrGetMyMessage")
	public APIResponse<?> hrgetmymes(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		return APIResponse.success("success get",mesService.hrGetMyMessage(tokenService.getCompanyNamefromToken(httpServletRequest.getHeader("Token"))));
	}
	
	/**
	* @author:吴嘉鑫 郑皓杰
	* @methodsName: empgetmymes
	* @description: emp获取自己信息
	* @param:  HttpServletRequest httpServletRequest
	* @param:  HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@ValidatedSending
	@CrossOrigin
	@RequestMapping("/empGetMyMessage")
	public APIResponse<?> empgetmymes(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		return APIResponse.success("success get",mesService.empGetMyMessage(tokenService.getUserNamefromToken(httpServletRequest.getHeader("token"))));
	}
	
	/**
	* @author:吴嘉鑫 郑皓杰
	* @methodsName: modifymes
	* @description: 修改自己信息
	* @param:  HttpServletRequest httpServletRequest
	* @param:  HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@ValidatedSending
	@CrossOrigin
	@RequestMapping("/modifyMessage")
	public APIResponse<?> modifymes(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		Integer mesid=Integer.parseInt(httpServletRequest.getParameter("mesid"));
		Integer salary=Integer.parseInt(httpServletRequest.getParameter("salary"));
		String position=httpServletRequest.getParameter("position");
		String text=httpServletRequest.getParameter("text");
		return mesService.modifyMessage(mesid,salary,position,text);
	}
	
	/**
	* @author:吴嘉鑫 郑皓杰
	* @methodsName: deletemes
	* @description: 删除自己信息
	* @param:  HttpServletRequest httpServletRequest
	* @param:  HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@ValidatedSending
	@CrossOrigin
	@RequestMapping("/deleteMessage")
	public APIResponse<?> deletemes(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		Integer mesid=Integer.parseInt(httpServletRequest.getParameter("mesid"));
		return mesService.deleteMessage(mesid);
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: getWordCloud
	* @description: 主界面获取关键词查询数据
	* @param:  HttpServletRequest httpServletRequest
	* @param:  HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/getCloud")
	public APIResponse<?> getWordCloud(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		return APIResponse.success("successget",mesService.getWordData());
	}

}
